function step01() {
    $("#flag").html("正在开启摄像头")
}

function step02() {
    $("#flag").html("请正视前方")
}

function openMedia() {
    let constraints = {
        video: {width: 500, height: 500},
        audio: false
    };
    //获得video摄像头
    let video = document.getElementById('video');
    let promise = navigator.mediaDevices.getUserMedia(constraints);
    promise.then((mediaStream) => {
        mediaStreamTrack = typeof mediaStream.stop === 'function' ? mediaStream : mediaStream.getTracks()[1];
        video.srcObject = mediaStream;
        video.play();
    });
}

// 拍照
function step03(url) {
    //获得Canvas对象
    let video = document.getElementById('video');
    let canvas = document.getElementById('canvas');
    let ctx = canvas.getContext('2d');
    ctx.drawImage(video, 0, 0, 500, 500);

    let img = document.getElementById('canvas').toDataURL();// toDataURL  图片数据流对象  默认'image/jpeg'

    $("#flag").html("正在识别");
    $.ajax({
        url: url,    //请求的url地址
        dataType: "json",   //返回格式为json
        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
        // contentType:"application/json",
        data: {"imagebast64": img}, //参数值
        type: "post", //请求方式
        success: function (response) {
            alert(response.msg);
            if (response.code === 200) {
                // 如果后台服务返回200状态，代表处理成功，这里继续执行后续的业务逻辑
                alert("用户id=" + response.userId);
                sessionStorage.setItem("user", JSON.stringify(response.user));
                window.location.href = "/index.html";
            } else {
                // 如果验证失败，刷新当前页重新验证
                window.location.reload();
            }
        }
    })
}

// 关闭摄像头
function closeMedia() {
    mediaStreamTrack.stop();
}